Page({
  data: {
    marketname: '',
    moduleList: '', 
    sku: '',
    addflage: 1
  },
  onShow: function () {
    this.initData();
    getApp().getCartNum(this);
    this.getIndexInfo();
    this.setData({
      marketname: wx.getStorageSync('supername')
    })
  },
  //初始化数据
  initData() {
    getApp().data.entryMode = "";
    getApp().data.shareSku = "";
  },
  bottomNav(e) {
    getApp(e).bottomNav(e);
  },
  //获得首页信息
  getIndexInfo() {
    var url = getApp().data.url + 'supermarket/getSupermarketIndex';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
    };
    var indeObj = wx.getStorageSync('indexObj');
    if (indeObj == "") {
      wx.showLoading({
        title: '',
        icon: 'loading'
      })
      getApp().wxAjax(this.makeIndexArr, url, body);
    } else {
      this.makeIndexArr(indeObj)
    }
  },
  makeIndexArr(res) {
    wx.hideLoading();
    wx.setStorageSync('indexObj', res)
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      var indexObj = resObj.body;
      var myModuleList = [];
      var moduleList = resObj.body.module_list;
      console.log(moduleList)
      for (let i = 0; i < moduleList.length; i++) {
        var obj = {};
        if (moduleList[i].content_type == 24 || moduleList[i].content_type == 7 || moduleList[i].content_type == 16 || moduleList[i].content_type == 20 || moduleList[i].content_type == 1) {
          obj = moduleList[i];
        }
        myModuleList.push(obj);
      }
      this.setData({
        moduleList: myModuleList
      })
      console.log(myModuleList)
      this.seckill();
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //秒杀显示
  seckill() {
    var seckillMod = {};
    for (let i = 0; i < this.data.moduleList.length; i++) {
      if (this.data.moduleList[i].content_type == 24) {
        var seckillMod = this.data.moduleList[i];
      }
    }
    console.log(seckillMod.content_obj)
    if (seckillMod.content_obj != undefined) {
      var endTime = seckillMod.content_obj.sec_kill_end_time;
      var nowTime = seckillMod.content_obj.now;
      //////////
      var nowTimeStr = nowTime.replace(/-/g, '/');
      var endTimeStr = endTime.replace(/-/g, '/');
      var nowTime2 = (new Date(nowTimeStr)).getTime();
      var endTime2 = (new Date(endTimeStr)).getTime();
      var cSpickeEndtimeDiff = endTime2 - nowTime2;
      //系统时间加上服务器商品秒杀结束时间差
      var mobTime = (new Date()).getTime();
      var mobEndTime = mobTime + cSpickeEndtimeDiff;
      console.log(mobEndTime)
      if (getApp().data.mobEndTime == "") {
        getApp().data.mobEndTime = mobEndTime;
      }
      getApp().getIndexActiveEndTime(this, getApp().data.mobEndTime, this.seckillOver);
    }
  },
  seckillOver() {
    getApp().getCartNum(this);
    this.getIndexInfo();
  },
  addGoods(e) {
    var skuid = e.currentTarget.dataset.skuid;
    this.setData({
      sku: skuid
    })
    getApp().addPro(this);
  },
  selsctMarket() {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  toPdetails(e) {
    var skuid = e.currentTarget.dataset.skuid;
    wx.navigateTo({
      url: '../pDetails/pDetails?skuid=' + skuid
    })
  },
  toclassification(e) {
    var link = e.currentTarget.dataset.link;
    var categoryid = link.split(',')[0].split('&')[1].split('=')[1];
    if (categoryid == 1) {
      categoryid = ""
    }
    wx.redirectTo({
      url: '../classification/classification?categoryid=' + categoryid,
    })
  },
  toSearch() {
    wx.navigateTo({
      url: '../search/search',
    })
  }
})